//
//  UserInfoManger.swift
//  SBNewProject
//
//  Created by hlt on 2023/9/13.
//

import UIKit
let UserInfoKey = "userInfoKey"

@objcMembers class UserInfoManger: NSObject {
    static var accountPath:String = {
        var accountPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
         accountPath += "/userAccount.plist"
        return accountPath
    }()
    
    static func loadUserInfo(){
        guard let data = try? Data(contentsOf: URL(fileURLWithPath:self.accountPath)) else{

            return
        }
        if  let model = try? NSKeyedUnarchiver.unarchivedObject(ofClass: UserInfo.self, from: data){
       
              UserInfo.shareInfo = model
        }else{
             UserInfo.shareInfo = UserInfo()
        }
    }
    
    static func saveUserInfo(_ user:UserInfo){

        if let data = try? NSKeyedArchiver.archivedData(withRootObject:user,  requiringSecureCoding:true){
            try? data.write(to: URL(fileURLWithPath:self.accountPath))
        }
    }
    
    static func loginOut(){
        UserInfo.shareInfo?.clear()
        try? FileManager.default.removeItem(atPath: self.accountPath)
        
    }
    
    static var login:Bool{
        if UserInfo.shareInfo?.token.count ?? 0  == 0{
            return false
        }
        return true
    }
    
}
